package cc.fs.study.pinyin;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

import cc.fs.study.R;

/**
 * Created by fostion on 6/6/16.
 */
public class PinyinAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int TYPE_TAB = 0;
    private final int TYPE_ITEM = 1;
    private List<Contacts> list;

    public PinyinAdapter(List<Contacts> _list) {
        this.list = _list;
        if (list != null) {
            Collections.sort(list);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_TAB) {
            return new TabViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pinyin_tab, parent, false));
        } else {
            return new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pinyin_normal, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof TabViewHolder) {
            ((TabViewHolder) holder).bind(list.get(position));
        } else {
            ((ItemViewHolder) holder).bind(list.get(position));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 || list.get(position).getFirstChar() != list.get(position - 1).getFirstChar()) {
            return TYPE_TAB;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class TabViewHolder extends RecyclerView.ViewHolder {
        TextView letterTV;
        TextView nameTV;

        public TabViewHolder(View itemView) {
            super(itemView);
            letterTV = (TextView) itemView.findViewById(R.id.letterTV);
            nameTV = (TextView) itemView.findViewById(R.id.nameTV);
        }

        public void bind(Contacts contacts) {
            letterTV.setText("" + contacts.getFirstChar());
            nameTV.setText(contacts.getName());
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView nameTV;

        public ItemViewHolder(View itemView) {
            super(itemView);
            nameTV = (TextView) itemView.findViewById(R.id.nameTV);
        }

        public void bind(Contacts contacts) {
            nameTV.setText(contacts.getName());
        }
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    public int getSectionForPosition(int position) {
        Contacts item = list.get(position);
        return item.getFirstChar();
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(String input) {
        if (TextUtils.isEmpty(input)) return -1;
        if (input.equals("#")) return 0;
        int section = input.charAt(0);
        for (int i = 0; i < getItemCount(); i++) {
            char firstChar = list.get(i).getFirstChar();
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }
}
